package com.cqeec.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import com.cqeec.action.LendingAction;
import com.cqeec.bean.LendingInfo;
import com.cqeec.util.ValidateUtil;

public class LendingFrame {
	static JInternalFrame lendingFrame = null;
	// 工具条
	static JToolBar toolBar = new JToolBar();
	static JButton borrowBtn = new JButton();
	static JButton backBtn = new JButton();
	static JButton optBtn = new JButton();
	static JButton findBtn = new JButton();
	static JButton returnBtn = new JButton();
	// 状态栏
	static JToolBar stateBar = new JToolBar();
	static JLabel lbState = new JLabel("状态");
	// 面板
	static JPanel mainPanl = new JPanel();
	static LendingListPanel listPanl = null;
	static LendingFindPanel findPanl = null;
	static LendingMainPanel lendingPanl = null;
	static String flag = "";
	static LendingInfo info = new LendingInfo();
	static LendingAction action = new LendingAction();

	public LendingFrame() {
	}

	public static void show(JDesktopPane deskPane) {
		if (lendingFrame == null) {
			lendingFrame = new JInternalFrame("借阅信息", false, true, false, true);
			lendingFrame.setSize(1000, 620);
			deskPane.add(lendingFrame);
			final Container c = lendingFrame.getContentPane();
			Init(c);
			lendingFrame.setVisible(true);
		}
		lendingFrame.addInternalFrameListener(new InternalFrameAdapter() {
			public void internalFrameClosing(InternalFrameEvent e) {
				// 关闭窗体前释放对象
				flag = "";
				lbState = null;
				listPanl = null;
				findPanl = null;
				lendingPanl = null;
				mainPanl = null;
				toolBar = null;
				borrowBtn = null;
				backBtn = null;
				optBtn = null;
				findBtn = null;
				returnBtn = null;
				stateBar = null;
				lbState = null;
				info = null;
				action = null;
				lendingFrame = null;
			}

		});
	}

	public static void Init(Container c) {
		lbState = new JLabel("状态");
		info = new LendingInfo();
		action = new LendingAction();
		mainPanl = new JPanel();
		toolBar = new JToolBar();
		borrowBtn = new JButton();
		borrowBtn.setToolTipText("借书");
		borrowBtn.setIcon(new ImageIcon("ico\\borrow.jpg"));
		borrowBtn.addActionListener(new LendingEventActionListener());
		toolBar.add(borrowBtn);

		backBtn = new JButton();
		backBtn.setToolTipText("还书");
		backBtn.setIcon(new ImageIcon("ico\\back.jpg"));
		backBtn.addActionListener(new LendingEventActionListener());
		toolBar.add(backBtn);

		optBtn = new JButton();
		optBtn.setToolTipText("操作");
		optBtn.setIcon(new ImageIcon("ico\\opt.jpg"));
		optBtn.addActionListener(new LendingEventActionListener());
		toolBar.add(optBtn);

		findBtn = new JButton();
		findBtn.setToolTipText("查询");
		findBtn.setIcon(new ImageIcon("ico\\find.jpg"));
		findBtn.addActionListener(new LendingEventActionListener());
		toolBar.add(findBtn);

		returnBtn = new JButton();
		returnBtn.setToolTipText("返回");
		returnBtn.setIcon(new ImageIcon("ico\\return.jpg"));
		returnBtn.addActionListener(new LendingEventActionListener());
		toolBar.add(returnBtn);
		c.setLayout(new BorderLayout());
		toolBar.setFloatable(false);
		c.add(toolBar, BorderLayout.NORTH);
		// 状态栏
		stateBar = new JToolBar();
		stateBar.add(lbState);
		stateBar.setFloatable(false);
		c.add(stateBar, BorderLayout.SOUTH);
		mainPanl.setLayout(new BorderLayout());

		listPanl = new LendingListPanel();
		listPanl.getLendingList(info);
		mainPanl.add(listPanl, BorderLayout.CENTER);
		c.add(mainPanl, BorderLayout.CENTER);
	}
}

class LendingEventActionListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == LendingFrame.findBtn) {
			find();
		}
		if (e.getSource() == LendingFrame.returnBtn) {
			ret();
		}
		if (e.getSource() == LendingFrame.borrowBtn) {
			borrow();
		}
		if (e.getSource() == LendingFrame.backBtn) {
			back();
		}
		if (e.getSource() == LendingFrame.optBtn) {
			operation();
		}
	}

	private void operation() {
		if (LendingFrame.lendingPanl != null) {
			if (LendingFrame.lendingPanl.txtReaderName.getText().equals("")) {
				JOptionPane.showMessageDialog(null, "读者信息为空", "读者信息",
						JOptionPane.WARNING_MESSAGE);
				LendingFrame.lendingPanl.txtReaderid.requestFocusInWindow();
				return;
			}
			if (LendingFrame.lendingPanl.txtBookName.getText().equals("")) {
				JOptionPane.showMessageDialog(null, "图书信息为空", "图书信息",
						JOptionPane.WARNING_MESSAGE);
				LendingFrame.lendingPanl.txtBookid.requestFocusInWindow();
				return;
			}
			String readerid = LendingFrame.lendingPanl.txtReaderid.getText()
					.trim();
			String bookid = LendingFrame.lendingPanl.txtBookid.getText().trim();
			int userid = MainFrame.userInfo.getUserid();

			if (LendingFrame.lendingPanl.flag.equals("borrow")) {
				// 借书前先检查当前图书是否还有
				if (LendingFrame.action.beforeBorrowCheckStockNum(bookid)) {
					if (LendingFrame.action.beforeBorrowOrReturn(readerid,
							bookid)) {
						if (LendingFrame.action.borrowBook(readerid, bookid,
								userid)) {
							JOptionPane.showMessageDialog(null, "操作成功", "图书借阅",
									JOptionPane.INFORMATION_MESSAGE);
							ret();
						} else {
							JOptionPane.showMessageDialog(null,
									"操作失败，请与系统管理员联系", "图书借阅",
									JOptionPane.WARNING_MESSAGE);
							return;
						}
					} else {
						JOptionPane.showMessageDialog(null,
								"此用户已借阅过此书暂未归还，不能重复借阅，操作失败", "图书借阅",
								JOptionPane.WARNING_MESSAGE);
						LendingFrame.lendingPanl.txtBookid
								.requestFocusInWindow();
						return;
					}
				} else {
					JOptionPane.showMessageDialog(null, "此图书已被其他读者借完，操作失败",
							"图书借阅", JOptionPane.WARNING_MESSAGE);
					LendingFrame.lendingPanl.txtBookid.requestFocusInWindow();
					return;
				}

			} else {
				if (LendingFrame.action.beforeBorrowOrReturn(readerid, bookid)) {
					JOptionPane.showMessageDialog(null, "此用户暂未借阅过此书或已归还，操作失败",
							"图书归还", JOptionPane.WARNING_MESSAGE);
					LendingFrame.lendingPanl.txtReaderid.requestFocusInWindow();
					return;
				} else {
					if (LendingFrame.action.returnBook(readerid, bookid)) {
						JOptionPane.showMessageDialog(null, "操作成功", "图书归还",
								JOptionPane.INFORMATION_MESSAGE);
						ret();
					} else {
						JOptionPane.showMessageDialog(null, "操作失败，请与系统管理员联系",
								"图书归还", JOptionPane.WARNING_MESSAGE);
						return;
					}
				}
			}
		}
	}

	private void borrow() {
		LendingFrame.lendingPanl = new LendingMainPanel("borrow");
		LendingFrame.mainPanl.removeAll();
		LendingFrame.mainPanl
				.add(LendingFrame.lendingPanl, BorderLayout.CENTER);
	}

	private void back() {
		LendingFrame.lendingPanl = new LendingMainPanel("back");
		LendingFrame.mainPanl.removeAll();
		LendingFrame.mainPanl
				.add(LendingFrame.lendingPanl, BorderLayout.CENTER);
	}

	private void ret() {
		LendingFrame.flag = "";
		LendingFrame.listPanl = new LendingListPanel();
		LendingFrame.listPanl.getLendingList(LendingFrame.info);
		LendingFrame.mainPanl.removeAll();
		LendingFrame.mainPanl.add(LendingFrame.listPanl, BorderLayout.CENTER);
	}

	private void find() {
		if (LendingFrame.flag.equals("query")) {
			if (LendingFrame.findPanl.cbLendDate.isSelected()) {
				if (!LendingFrame.findPanl.txtstartLendDate.getText().trim()
						.equals("")) {
					if (!ValidateUtil
							.StringToDate(LendingFrame.findPanl.txtstartLendDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null,
								"输入的日期格式有误，请按yyyy-MM-dd格式输入", "借阅信息",
								JOptionPane.ERROR_MESSAGE);
						LendingFrame.findPanl.txtstartLendDate
								.requestFocusInWindow();
						return;
					}
				}
				LendingFrame.info
						.setStartlendDate(LendingFrame.findPanl.txtstartLendDate
								.getText().trim());
				if (!LendingFrame.findPanl.txtendLendDate.getText().trim()
						.equals("")) {
					if (!ValidateUtil
							.StringToDate(LendingFrame.findPanl.txtendLendDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null,
								"输入的日期格式有误，请按yyyy-MM-dd格式输入", "借阅信息",
								JOptionPane.ERROR_MESSAGE);
						LendingFrame.findPanl.txtendLendDate
								.requestFocusInWindow();
						return;
					}
				}
				LendingFrame.info
						.setEndlendDate(LendingFrame.findPanl.txtendLendDate
								.getText().trim());
			} else {
				LendingFrame.info.setStartlendDate("");
				LendingFrame.info.setEndlendDate("");
			}

			if (LendingFrame.findPanl.cbBackDate.isSelected()) {
				if (!LendingFrame.findPanl.txtstartBackDate.getText().trim()
						.equals("")) {
					if (!ValidateUtil
							.StringToDate(LendingFrame.findPanl.txtstartBackDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null,
								"输入的日期格式有误，请按yyyy-MM-dd格式输入", "借阅信息",
								JOptionPane.ERROR_MESSAGE);
						LendingFrame.findPanl.txtstartBackDate
								.requestFocusInWindow();
						return;
					}
				}
				LendingFrame.info
						.setStartbackDate(LendingFrame.findPanl.txtstartBackDate
								.getText().trim());
				if (!LendingFrame.findPanl.txtendBackDate.getText().trim()
						.equals("")) {
					if (!ValidateUtil
							.StringToDate(LendingFrame.findPanl.txtendBackDate
									.getText().trim())) {// 验证数据类型
						JOptionPane.showMessageDialog(null,
								"输入的日期格式有误，请按yyyy-MM-dd格式输入", "借阅信息",
								JOptionPane.ERROR_MESSAGE);
						LendingFrame.findPanl.txtendBackDate
								.requestFocusInWindow();
						return;
					}
				}
				LendingFrame.info
						.setEndbackDate(LendingFrame.findPanl.txtendBackDate
								.getText().trim());
			} else {
				LendingFrame.info.setStartbackDate("");
				LendingFrame.info.setEndbackDate("");
			}

			if (LendingFrame.findPanl.cbIsbn.isSelected()) {
				LendingFrame.info.setBookid(LendingFrame.findPanl.txtIsbn
						.getText().trim());
			} else {
				LendingFrame.info.setBookid("");
			}
			if (LendingFrame.findPanl.cbName.isSelected()) {
				LendingFrame.info.setBookname(LendingFrame.findPanl.txtName
						.getText().trim());
			} else {
				LendingFrame.info.setBookname("");
			}
			if (LendingFrame.findPanl.cbType.isSelected()) {
				LendingFrame.info.setBooktype(LendingFrame.findPanl.Type
						.getSelectedItem().toString().trim());
			} else {
				LendingFrame.info.setBooktype("");
			}
			if (LendingFrame.findPanl.cbReaderid.isSelected()) {
				LendingFrame.info.setReaderid(LendingFrame.findPanl.txtReaderid
						.getText().trim());
			} else {
				LendingFrame.info.setReaderid("");
			}
			if (LendingFrame.findPanl.cbReadername.isSelected()) {
				LendingFrame.info
						.setReadername(LendingFrame.findPanl.txtReadername
								.getText().trim());
			} else {
				LendingFrame.info.setReadername("");
			}
			if (LendingFrame.findPanl.cbTel.isSelected()) {
				LendingFrame.info.setTel(LendingFrame.findPanl.txtTel.getText()
						.trim());
			} else {
				LendingFrame.info.setTel("");
			}
			if (LendingFrame.findPanl.cbOptor.isSelected()) {
				LendingFrame.info.setRealname(LendingFrame.findPanl.txtOptor
						.getText().trim());
			} else {
				LendingFrame.info.setRealname("");
			}

			ret();
			LendingFrame.flag = "";
			return;
		}
		LendingFrame.findPanl = new LendingFindPanel();
		LendingFrame.mainPanl.removeAll();
		LendingFrame.mainPanl.add(LendingFrame.findPanl, BorderLayout.CENTER);
		LendingFrame.flag = "query";
	}
}
